package com.lc1993929.mr.rating.efficienttopn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.codehaus.jackson.map.ObjectMapper;

import java.io.IOException;

/**
 * Created by LiuChang on 2017/12/19/019.
 */
public class RateTopN {

    static class RateTopNMapper extends Mapper<LongWritable, Text, RateBean, NullWritable> {
        ObjectMapper objectMapper;

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            objectMapper = new ObjectMapper();

        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            RateBean rateBean = objectMapper.readValue(value.toString(), RateBean.class);
            context.write(rateBean, NullWritable.get());
        }
    }


    static class RateTopNReducer extends Reducer<RateBean, NullWritable, RateBean, NullWritable> {
        @Override
        protected void reduce(RateBean key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
            int topn = context.getConfiguration().getInt("rate.top.n", 3);
            int count = 0;
            for (NullWritable value : values) {
                context.write(key, NullWritable.get());
                count++;
                if (count == topn) {
                    return;
                }
            }
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //向mapreduce内部各组件传递参数
        conf.addResource("userconf.xml");
        //conf.set("user.rate.topn", args[0]);

        //conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf, "topndemo");
        //要执行的jar文件
        job.setJarByClass(RateTopN.class);
        //指定mapper类和reducer类
        job.setMapperClass(RateTopNMapper.class);
        job.setReducerClass(RateTopNReducer.class);
        //指定mapper和reducer的输出的key和value的数据类型
        job.setMapOutputKeyClass(RateBean.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(RateBean.class);
        job.setOutputValueClass(NullWritable.class);

        //设置分区规则
        job.setPartitionerClass(UidPartitioner.class);
        //设置排序规则
        job.setGroupingComparatorClass(UidGroupingComparator.class);
        //指定要解析的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[1]));
        //指定输出的文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[2]));

        //设置reduce任务个数
        job.setNumReduceTasks(1);
        // 向yarn提交job
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);
    }
}
